/*
 * > /*
>  * INSANE - Interactive Structural Analysis Environment
>  *
>  * Copyright (C) 2003-2004
>  * Universidade Federal de Minas Gerais
>  * Escola de Engenharia
>  * Departamento de Engenharia de Estruturas
>  *
>  * Author's email :     insane@dees.ufmg.br
>  * Author's Website :   http://www.dees.ufmg.br/insane
>  *
>  * This program is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU General Public License
>  * as published by the Free Software Foundation; either version 2
>  * of the License, or any later version.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>  * GNU General Public License for more details.
>  *
>  * You should have received a copy of the GNU General Public License
>  * along with this program; if not, write to the Free Software
>  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
> 1307, USA.
 *
 */
package br.ufmg.dees.insane.ui.rich.learn.learn_view.equation_view;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JToolBar;
import javax.swing.text.Position;

import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.ui.rich.learn.learn_controller.EquationsController;
import br.ufmg.dees.insane.ui.rich.learn.learn_controller.GridEquationsController;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.CmdButton;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.CmdMenu;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.DrawingArea;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.IView;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnInterface;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnView;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.command.Command;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.command.SelectCommand;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.equation_view.command.AutomaticEquationsCommand;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.equation_view.command.EquationsSettingsCommand;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.equation_view.command.Help0Command;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.equation_view.command.NodeByNodeEquationsCommand;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.equation_view.command.ShowSelectedEquationsCommand;

/**
*
* @author Moreira, Renata & Pitangueira, Roque
* @since May 2005
*/
public class EquationsView extends LearnView {  
	
//  No da arvore: Equations
	private Command equationsSettingsCmd = new EquationsSettingsCommand(parent);
	
	private Command automaticEquationsCmd = new AutomaticEquationsCommand(parent);
	private Command showSelectedEquationsCmd = new ShowSelectedEquationsCommand(parent);
	private Command nodeByNodeEquationsCmd = new NodeByNodeEquationsCommand(parent);
	private Command help0Cmd = new Help0Command(parent);
	
//	No Equations
	protected CmdMenu itemEquationsSettings = new CmdMenu("Equations Settings");
	
    private CmdMenu itemAutomaticEquations = new CmdMenu("Automatic Equations", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Eq11.gif")));
    private CmdMenu itemShowSelectedEquations = new CmdMenu("Show Selected Equations", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/EqSel.gif")));
    private CmdMenu itemNodeByNodeEquations = new CmdMenu("Node by Node Equations", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/EqNo.gif")));
    private CmdMenu itemHelp0 = new CmdMenu("Help", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Help16.gif")));

    
    //No Equations
    private CmdButton automaticEquations = new CmdButton(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Eq11.gif")));
    private CmdButton showSelectedEquations = new CmdButton(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/EqSel.gif")));
    private CmdButton nodeByNodeEquations = new CmdButton(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/EqNo.gif")));
    private CmdButton help0 = new CmdButton(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Help16.gif")));
   
    private JMenu menuEquations = new JMenu("Equations");
    private JToolBar equationsToolBar = new JToolBar();
    
	public EquationsView(LearnInterface parent, FemModel m){
	    
		super(parent, m);
		super.setViewState(new EquationsViewState());
		super.setDrawingArea(new DrawingArea((IView)this));
		super.setController(new EquationsController(this));
		
		String gAnlType = m.getGlobalAnalysisModel().getType();
		
		if (!(gAnlType.equals("Grid"))) {
			super.setController(new EquationsController(this));
		} else {
			super.setController(new GridEquationsController(this));
		}
		
		
		
		//Setting menu Equations
		menuSettings.add(itemEquationsSettings);
		itemEquationsSettings.setCommand(equationsSettingsCmd);
		itemEquationsSettings.setMnemonic(KeyEvent.VK_E);
		itemEquationsSettings.addActionListener(parent);
		
        menuEquations.add(itemAutomaticEquations);
        itemAutomaticEquations.setCommand(automaticEquationsCmd);
        itemAutomaticEquations.setEnabled(false);
        itemAutomaticEquations.setMnemonic(KeyEvent.VK_E);
        itemAutomaticEquations.addActionListener(parent);
        
        menuEquations.add(itemShowSelectedEquations);
        itemShowSelectedEquations.setCommand(showSelectedEquationsCmd);
        itemShowSelectedEquations.setEnabled(false);
        itemShowSelectedEquations.setMnemonic(KeyEvent.VK_S);
        itemShowSelectedEquations.addActionListener(parent);
        
        menuEquations.add(itemNodeByNodeEquations);
        itemNodeByNodeEquations.setCommand(nodeByNodeEquationsCmd);
        itemNodeByNodeEquations.setEnabled(false);
        itemNodeByNodeEquations.setMnemonic(KeyEvent.VK_N);
        itemNodeByNodeEquations.addActionListener(parent);
        
        menuEquations.add(itemHelp0);
        itemHelp0.setCommand(help0Cmd);
        itemHelp0.setEnabled(false);
        itemHelp0.setMnemonic(KeyEvent.VK_H);
        itemHelp0.addActionListener(parent);
        
        equationsToolBar.add(automaticEquations);
        automaticEquations.setToolTipText("Automatic Equations");
        automaticEquations.addActionListener(parent);
        automaticEquations.setCommand(automaticEquationsCmd);
        
        equationsToolBar.add(showSelectedEquations);
        showSelectedEquations.setToolTipText("Show Selected Equations");
        showSelectedEquations.addActionListener(parent);
        showSelectedEquations.setCommand(showSelectedEquationsCmd);
        
        equationsToolBar.add(nodeByNodeEquations);
        nodeByNodeEquations.setToolTipText("Node by Node Equations");
        nodeByNodeEquations.addActionListener(parent);
        nodeByNodeEquations.setCommand(nodeByNodeEquationsCmd);
        
        equationsToolBar.add(help0);
		help0.setToolTipText("Help");
		help0.addActionListener(parent);
		help0.setCommand(help0Cmd);
        
	}
	
	public void init(){
	    
		super.init();
			parent.getMenuPanel().removeAll();
			parent.getMenuPanel().add(menuFile);
			itemSave.setEnabled(true);
			itemSaveAs.setEnabled(true);
			itemClose.setEnabled(true);
			itemPrint.setEnabled(true);
			menuExport.setEnabled(true);
			menuImport.setEnabled(true);
			parent.getMenuPanel().add(menuEdit);
			parent.getMenuPanel().add(menuView);
			parent.getMenuPanel().add(menuEquations);
			menuEquations.setEnabled(true);
			itemAutomaticEquations.setEnabled(true);
			itemShowSelectedEquations.setEnabled(true);
			itemNodeByNodeEquations.setEnabled(true);
			itemHelp0.setEnabled(true);
			parent.getMenuPanel().add(menuSettings);
			parent.getMenuPanel().add(menuHelp);
	        
			parent.getToolPanel().removeAll();
			parent.getToolPanel().add(fileToolBar);
			save.setEnabled(true);
			saveAs.setEnabled(true);
			print.setEnabled(true);
			delete.setEnabled(false);
			parent.getToolPanel().add(editToolBar);
			parent.getToolPanel().add(zoomToolBar);
			parent.getToolPanel().add(equationsToolBar);
			automaticEquations.setEnabled(true);
			showSelectedEquations.setEnabled(true);
			nodeByNodeEquations.setEnabled(true);
			help0.setEnabled(true);
							
	        parent.getConsolePanel().setVisible(true);
			
			grid.setEnabled(true);
			snap.setEnabled(true);
			
			this.getDrawingArea().setRepainting(false);
			this.getDrawingArea().repaint();
	        
			parent.getConsoleHandler().setToCommandMode();
			parent.getConsoleHandler().setKeybCommand(null);
			
			this.setStateButtons();
			
			parent.getTree().setEnabled(true);
			parent.getTree().setSelectionPath(parent.getTree().getNextMatch("Equations", 0, Position.Bias.Forward));
			
			this.getController().compose();

			parent.getDrawPanel().removeAll();
			parent.getDrawPanel().add(this.getDrawingArea(), BorderLayout.CENTER);
			this.getDrawingArea().removeAllCommands();
			this.getDrawingArea().setClickedCommand(new SelectCommand(parent));
			this.getDrawingArea().setRightClickedCommand(new SelectCommand(parent));
			this.getDrawingArea().setCursor();
			parent.getDrawPanel().repaint();
			parent.getDrawPanel().updateUI();
			
			parent.getJMenuBar().updateUI();
			parent.repaint();
	    }   

	public  JFrame getInterface(){
    	return parent;
    }
    
	public void setInterface(JFrame par){
    	this.parent = (LearnInterface)par;
    }

    public void setCoord(String x, String y){
    	this.parent.setCoord(x, y);
    }
    
    public void setMessage(String m){
    	this.parent.setMessage(m);
    }

}
